package com.bojiu.lawsearch.lawsearch.util;

import com.bojiu.lawsearch.lawsearch.model.WechatUser;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.TreeMap;

/**
 * @Author afdiao
 * @Date 2020/2/25 23:36
 */
@Component
@Slf4j
public class HttpUtil {

    @Autowired
    RestTemplate restTemplate;

    public JsonObject doGet(String url) {
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        String body = forEntity.getBody();
        JsonObject jsonObject = new Gson().fromJson(body, JsonObject.class);
        return jsonObject;
    }


//    /**
//     * 根据code获取用户详情
//     *
//     * @param code
//     * @return
//     */
//    public WechatUser getUser(String code) {
//        String access_url = String.format(Constances.OPENID_URL, Constances.APPID, Constances.APPSECRET, code);
//        log.debug("access_url is :{}", access_url);
//        Gson gson = new Gson();
//        ResponseEntity<String> forEntity = restTemplate.getForEntity(access_url, String.class);
//        String body = forEntity.getBody();
//        if (StringUtils.isEmpty(body)) {
//            log.error("error to get access_token");
//            return null;
//        }
//        JsonObject jsonObject = gson.fromJson(body, JsonObject.class);
//        String access_token = jsonObject.get("access_token").getAsString();
//        String openid = jsonObject.get("openid").getAsString();
//
//        //开始获取用户明细
//        String userInfo_url = String.format(Constances.USERINFO_URL, access_token, openid);
//        ResponseEntity<String> stringResponseEntity = restTemplate.getForEntity(userInfo_url, String.class);
//        String userInfoBody = stringResponseEntity.getBody();
//        if (StringUtils.isEmpty(userInfoBody)) {
//            log.error("error to get userInfo");
//            return null;
//        }
//        JsonObject userObj = gson.fromJson(userInfoBody, JsonObject.class);
//        String nickname = userObj.get("nickname").getAsString();
//        String sex = userObj.get("sex").getAsString();
//        //为空性别默认为男
//        sex = StringUtils.isEmpty(sex) ? "1" : sex;
//        String headimgurl = userObj.get("headimgurl").getAsString();
//        WechatUser wechatUser = new WechatUser();
//        wechatUser.setNickName(nickname);
//        wechatUser.setHeadUrl(headimgurl);
//        wechatUser.setOpenid(openid);
//        wechatUser.setSex(sex);
//        return wechatUser;
//    }
}
